/*:
 * @plugindesc 战斗音效-行动、受伤、死亡 播放SE
 * @author 小c 老饕
 * 
 * @param Voice pitch
 * @type number
 * @desc SE音调
 * @default 100
 *
 * @param Voice Volume
 * @type number
 * @desc SE音量
 * @default 90
 *
 * @param Voice Pan
 * @type number
 * @desc SE声像
 * @default 0
 *
 * @param Action Voice Timeout
 * @type number
 * @desc 技能音效延时
 * 默认值：0
 * @default 0
 *
 * @param Dead Voice Timeout
 * @type number
 * @desc 死亡音效延时
 * 默认值：0
 * @default 0
 * 
 * @help
 * 为战斗者提供独立的战斗音效
 * 以下用于角色/敌人的备注中
 * <Attack Voice: name>普攻
 * <Skill Voice: name>普通技能
 * <Friend Voice: name>目标我方的技能【治疗、BUFF等】
 * <Damage Voice: name>受伤
 * <Dead Voice: name>死亡
 *
 * PS. 技能的部分配置会被YEP战斗序列插件抵消，此时请在技能中按照战斗序列
 * 的要求配置SE
 * 
 * 老饕：死亡时不播放受伤音效。分身死亡时不播放音效
 */

var BattlerVoiceManager = BattlerVoiceManager || {};
var Imported = Imported || {};
Imported.CP_BattlerVoices = true;

var params = PluginManager.parameters("CP_BattlerVoices");

BattlerVoiceManager.VOICE_PITCH = Number(params["Voice pitch"]) || 100;
BattlerVoiceManager.VOICE_VOLUME = Number(params["Voice Volume"]) || 90;
BattlerVoiceManager.VOICE_PAN = Number(params["Voice Pan"]) || 0;
BattlerVoiceManager.ACTION_VOICE_TIMEOUT = Number(params["Action Voice Timeout"]) || 0;
BattlerVoiceManager.DEAD_VOICE_TIMEOUT = Number(params["Dead Voice Timeout"]) || 0;

BattlerVoiceManager.TYPE_ATTACK = 1;
BattlerVoiceManager.TYPE_SKILL = 2 ;
BattlerVoiceManager.TYPE_FRIEND = 3;
BattlerVoiceManager.TYPE_DAMAGE = 4;
BattlerVoiceManager.TYPE_DEAD = 5;

BattlerVoiceManager.getSeFromName = function(name){
	var se = {};
	se.name = name.trim();
	se.pitch = BattlerVoiceManager.VOICE_PITCH;
	se.volume = BattlerVoiceManager.VOICE_VOLUME;
	se.pan = BattlerVoiceManager.VOICE_PAN;
	return se;
};

BattlerVoiceManager.DATABASE_LOADED = DataManager.isDatabaseLoaded;
DataManager.isDatabaseLoaded = function(){
	var loaded = BattlerVoiceManager.DATABASE_LOADED.call(this);
	if(!loaded)
		return loaded;

	BattlerVoiceManager.loadVoiceData();
	return loaded;
};

BattlerVoiceManager.loadBattlerVoiceData = function(battler){
		battler.attackVoice = null;
		battler.skillVoice = null;
		battler.friendVoice = null;
		battler.damageVoice = null;
		battler.deadVoice = null;

		if(battler.meta["Attack Voice"]){
			var name = battler.meta["Attack Voice"];
			battler.attackVoice = BattlerVoiceManager.getSeFromName(name);
		}

		if(battler.meta["Skill Voice"]){
			var name = battler.meta["Skill Voice"];
			battler.skillVoice = BattlerVoiceManager.getSeFromName(name);
		}

		if(battler.meta["Friend Voice"]){
			var name = battler.meta["Friend Voice"];
			battler.friendVoice = BattlerVoiceManager.getSeFromName(name);
		}

		if(battler.meta["Damage Voice"]){
			var name = battler.meta["Damage Voice"];
			battler.damageVoice = BattlerVoiceManager.getSeFromName(name);
		}

		if(battler.meta["Dead Voice"]){
			var name = battler.meta["Dead Voice"];
			battler.deadVoice = BattlerVoiceManager.getSeFromName(name);
		}
};

BattlerVoiceManager.loadVoiceData = function(){
	for(var i = 1; i < $dataActors.length; i++){
		var actor = $dataActors[i];
		BattlerVoiceManager.loadBattlerVoiceData(actor);
	}

	for(var i = 1; i < $dataEnemies.length; i++){
		var enemy = $dataEnemies[i];
		BattlerVoiceManager.loadBattlerVoiceData(enemy);
	}
};

BattlerVoiceManager.playBattlerVoice = function(battler, type){
	var se = null;
	switch(type){
		case BattlerVoiceManager.TYPE_ATTACK:
			se = battler.attackVoice;
			break;
		case BattlerVoiceManager.TYPE_SKILL:
			se = battler.skillVoice;
			break;
		case BattlerVoiceManager.TYPE_FRIEND:
			se = battler.friendVoice;
			break;
		case BattlerVoiceManager.TYPE_DAMAGE:
			se = battler.damageVoice;
			break;
		case BattlerVoiceManager.TYPE_DEAD:
			se = battler.deadVoice;
			break;
	}

	if(se)
		AudioManager.playSe(se);
}

BattlerVoiceManager.PERFORM_ACTION = Game_Battler.prototype.performAction;
Game_Battler.prototype.performAction = function(action) {
	BattlerVoiceManager.PERFORM_ACTION.call(this, action)
	var battler = null;
	if(this.isActor())
		battler = this.actor();
	else
		battler = this.enemy();

	setTimeout(function(action, battler){
		if(action.isGuard())
			return;
		if (action.isAttack() || (!action.isForFriend() && action.isItem()))
    		BattlerVoiceManager.playBattlerVoice(battler, BattlerVoiceManager.TYPE_ATTACK);
    	else if (action.isSkill() && !action.isForFriend())
    		BattlerVoiceManager.playBattlerVoice(battler, BattlerVoiceManager.TYPE_SKILL);
    	else if (action.isForFriend()){
			if (!(action.isSkill() && action.item().id == 3)){	//逃跑不播放音效
				BattlerVoiceManager.playBattlerVoice(battler, BattlerVoiceManager.TYPE_FRIEND);
			}
		}
    		
	}, BattlerVoiceManager.ACTION_VOICE_TIMEOUT, action, battler);
};

BattlerVoiceManager.PERFORM_DAMAGE = Game_Battler.prototype.performDamage;
Game_Battler.prototype.performDamage = function() {
	BattlerVoiceManager.PERFORM_DAMAGE.call(this);

	var battler = null;
	if(this.isActor())
		battler = this.actor();
	else
		battler = this.enemy();
	// 老饕：死亡时时不播放受伤音效
	if (this._hp > 0)
		BattlerVoiceManager.playBattlerVoice(battler, BattlerVoiceManager.TYPE_DAMAGE);
};

BattlerVoiceManager.PERFORM_COLLAPSE = Game_Battler.prototype.performCollapse;
Game_Battler.prototype.performCollapse = function() {
	BattlerVoiceManager.PERFORM_COLLAPSE.call(this);

	var battler = null;
	if(this.isActor())
		battler = this.actor();
	else
		battler = this.enemy();
	// 老饕：分身、死亡效果为“逃跑”者不播放死亡音效
	if(!this.isInvincibleByDoppelganger() && !(this._collpaseData[0] == 11) ){
		setTimeout(function(battler){
			BattlerVoiceManager.playBattlerVoice(battler, BattlerVoiceManager.TYPE_DEAD);
		}, BattlerVoiceManager.DEAD_VOICE_TIMEOUT, battler);
	}
};